import Vue from 'vue'
import request from 'axios'
import {WechatPlugin} from 'vux'
import store from 'store/store' 
// import wxBaseConfig from './wxBase.js'
import {appId,loadSignatureUrl} from '@/config/config'
export default function(userConfig){
    let wx = WechatPlugin.$wechat
    let origUrl = location.href.split('#')[0]
    let url = encodeURIComponent(origUrl)
    let noncestr = new Date().getTime()+''
    let timestr = noncestr+''
    if(timestr.length>10){
        timestr = timestr.substring(0,10)
    }
    // 默认分享内容
    let defaultConfig = {
      title: '浔龙河麦佶岛小镇商城',
      desc: '浔龙河麦佶岛小镇商城',
      link: 'http://www.zlxlhwl.com/wx-mall/',
      imgUrl:'http://39.108.188.252:8765/shareImg/mj.jpg',
      success: function () {
        Vue.$vux.toast.show({
          text: '感谢您的分享！',
          time: 1000,
        })
      }
    };
    let wxConfig = Object.assign({}, defaultConfig, userConfig);
    let data = `noncestr=${noncestr}&timestamp=${timestr}&url=${url}`
    request.post(loadSignatureUrl+'/signature',data,{headers:{'Content-Type': 'application/x-www-form-urlencoded'}})
      .then((res)=>{
        if(res.data.status == '200'){
          wx.config({
            // debug: true,
            appId: appId,
            timestamp: timestr, // 必填，生成签名的时间戳
            nonceStr: noncestr, // 必填，生成签名的随机串
            signature: res.data.data,
            jsApiList: [
              'onMenuShareAppMessage',
              'onMenuShareTimeline',
              // 'hideAllNonBaseMenuItem',
              'getLocation',
              'scanQRCode',
              'chooseWXPay'
            ]
          })
          wx.ready(function(){
            wx.getLocation({
              type: 'wgs84',
              success:function(res){
                // 通过vuex来调用坐标
                store.dispatch('getLocation',JSON.stringify(res))
              }
            })
            // wx.hideAllNonBaseMenuItem();
            wx.showMenuItems({
              menuList: [
                'menuItem:share:appMessage',
                'menuItem:share:timeline'
              ]
            });
            //分享给朋友
            wx.onMenuShareAppMessage(wxConfig)
            //分享到朋友圈
            wx.onMenuShareTimeline(wxConfig)
          })
        }else{
          Vue.$vux.toast.show({
            type: 'warn',
              text: '微信签名失败',
              time: 2000
            })
          }
        }).catch(err=>{
            // console.log("fetch fail", JSON.stringify(err));
            Vue.$vux.toast.show({
              type: 'warn',
                text: '微信签名失败',
                time: 2000
              })
        })
}